﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using IBS.Webparts.PostIt.Code.Presenters;

namespace IBS.Webparts.PostIt.PostItWebpart
{
    /// <summary>
    /// Editor Part for rendering the custom PostIt Webpart Properties
    /// </summary>
    public class PostItPropertiesEditorPart : EditorPart
    {
        /// <summary>
        /// Displays the list of SharePoint Lists to choose from
        /// </summary>
        private DropDownList List
        {
            get
            {
                EnsureChildControls();
                return _list;
            }
        }

        private DropDownList _list;


        /// <summary>
        /// Displays the list of fields to use as the Title
        /// </summary>
        //private DropDownList TitleField
        //{
        //    get
        //    {
        //        EnsureChildControls();
        //        return _titleField;
        //    }
        //}

        //private DropDownList _titleField;


        /// <summary>
        /// Displays the list of fields to use as the StartDate
        /// </summary>
        //private DropDownList StartDateField
        //{
        //    get
        //    {
        //        EnsureChildControls();
        //        return _startDateField;
        //    }
        //}

        //private DropDownList _startDateField;


        /// <summary>
        /// Displays the list of fields to use as the EndDate
        /// </summary>
        //private DropDownList EndDateField
        //{
        //    get
        //    {
        //        EnsureChildControls();
        //        return _endDateField;
        //    }
        //}

        //private DropDownList _endDateField;


        /// <summary>
        /// Displays a TextBox to enter the Title field 
        /// </summary>
        private TextBox TitleField
        {
            get
            {
                EnsureChildControls();
                return _titleField;
            }
        }

        private TextBox _titleField;


        /// <summary>
        /// Displays a TextBox to enter the Start Date field
        /// </summary>
        private TextBox StartDateField
        {
            get
            {
                EnsureChildControls();
                return _startDateField;
            }
        }

        private TextBox _startDateField;


        /// <summary>
        /// Displays a TextBox to enter the End Date field
        /// </summary>
        private TextBox EndDateField
        {
            get
            {
                EnsureChildControls();
                return _endDateField;
            }
        }

        private TextBox _endDateField;


        /// <summary>
        /// Displays the text box for Pause Time
        /// </summary>
        private TextBox PauseTime
        {
            get
            {
                EnsureChildControls();
                return _pauseTime;
            }
        }

        private TextBox _pauseTime;


        /// <summary>
        /// Whether to include a reference to the jQuery library 
        /// </summary>
        private CheckBox IncludejQueryReference
        {
            get
            {
                EnsureChildControls();
                return _includejQueryReference;
            }
        }

        private CheckBox _includejQueryReference;


        /// <summary>
        /// Displays the Textbox for the jQuery path
        /// </summary>
        private TextBox jQueryPath
        {
            get
            {
                EnsureChildControls();
                return _jQueryPath;
            }
        }

        private TextBox _jQueryPath;


        /// <summary>
        /// Whether to include a reference to the bxSlider library 
        /// </summary>
        private CheckBox IncludebxSliderReference
        {
            get
            {
                EnsureChildControls();
                return _includebxSliderReference;
            }
        }

        private CheckBox _includebxSliderReference;


        /// <summary>
        /// Displays the Textbox for the bxSlider path
        /// </summary>
        private TextBox bxSliderPath
        {
            get
            {
                EnsureChildControls();
                return _bxSliderPath;
            }
        }

        private TextBox _bxSliderPath;


        /// <summary>
        /// Displays the Textbox for the CSS path
        /// </summary>
        private TextBox CssPath
        {
            get
            {
                EnsureChildControls();
                return _cssPath;
            }
        }

        private TextBox _cssPath;



        /// <summary>
        /// Presenter object which facilitates communication to external components
        /// </summary>
        private readonly PostItWebpartPresenter _presenter;


        /// <summary>
        /// Create a new instance of the PostItPropertiesEditorPart class
        /// </summary>
        /// <param name="id">ID of the Webpart to edit</param>
        /// <param name="presenter">PostIt Webpart Presenter</param>
        public PostItPropertiesEditorPart(string id, PostItWebpartPresenter presenter)
        {
            _presenter = presenter;
            ID = "PostItPropertiesEditorPart_" + id;
            Title = "Post-It Webpart";
        }


        /// <summary>
        /// Retrieve the values from the page and save them to the Webpart control
        /// </summary>
        /// <returns>Whether changes were applied</returns>
        public override bool ApplyChanges()
        {
            var webpart = WebPartToEdit as PostItWebpart;

            if (webpart != null)
            {
                webpart.ListGuid = new Guid(List.SelectedValue);
                //webpart.TitleField = TitleField.SelectedValue;
                //webpart.StartDateField = StartDateField.SelectedValue;
                //webpart.EndDateField = EndDateField.SelectedValue;
                webpart.TitleField = TitleField.Text;
                webpart.StartDateField = StartDateField.Text;
                webpart.EndDateField = EndDateField.Text;

                webpart.PauseTime = Convert.ToInt32(PauseTime.Text);
                webpart.IncludejQueryReference = IncludejQueryReference.Checked;
                webpart.jQueryPath = jQueryPath.Text;
                webpart.IncludebxSliderReference = IncludebxSliderReference.Checked;
                webpart.bxSliderPath = bxSliderPath.Text;

                webpart.SaveChanges();
                return true;
            }

            return false;
        }


        /// <summary>
        /// Synchronize changes made to the Webpart with the editor
        /// </summary>
        public override void SyncChanges()
        {
            EnsureChildControls();
            var part = WebPartToEdit as PostItWebpart;

            // Select the current list in the drop-down control
            var selectedList = part.ListGuid.ToString();
            foreach (ListItem item in List.Items)
            {
                if (item.Value == selectedList)
                {
                    item.Selected = true;
                    break;
                }
            }


            // Select the current title field in the drop-down control
            //var selectedTitleField = part.TitleField;
            //foreach (ListItem item in TitleField.Items)
            //{
            //    if (item.Value == selectedTitleField)
            //    {
            //        item.Selected = true;
            //        break;
            //    }
            //}


            // Select the current start date field in the drop-down control
            //var selectedStartDateField = part.StartDateField;
            //foreach (ListItem item in StartDateField.Items)
            //{
            //    if (item.Value == selectedStartDateField)
            //    {
            //        item.Selected = true;
            //        break;
            //    }
            //}


            // Select the current end date field in the drop-down control
            //var selectedEndDateField = part.EndDateField;
            //foreach (ListItem item in EndDateField.Items)
            //{
            //    if (item.Value == selectedEndDateField)
            //    {
            //        item.Selected = true;
            //        break;
            //    }
            //}


            // Display the current Title Field in the text box
            TitleField.Text = part.TitleField;


            // Display the current Start Date Field in the text box
            StartDateField.Text = part.StartDateField;


            // Display the current End Date Field in the text box
            EndDateField.Text = part.EndDateField;


            // Display the current Pause Time in the text box
            PauseTime.Text = part.PauseTime.ToString();


            // Display the IncludejQueryRefrence state
            IncludejQueryReference.Checked = part.IncludejQueryReference;


            // Display the current jQuery path in the text box
            jQueryPath.Text = part.jQueryPath;


            // Display the IncludebxSliderRefrence state
            IncludebxSliderReference.Checked = part.IncludebxSliderReference;


            // Display the current bxSlider path in the text box
            bxSliderPath.Text = part.bxSliderPath;


            // Display the current CSS path in the text box
            CssPath.Text = part.CssPath;
        }


        /// <summary>
        /// Create the child controls to be displayed in the UI
        /// </summary>
        protected override void CreateChildControls()
        {
            var part = WebPartToEdit as PostItWebpart;

            Controls.Clear();

            Dictionary<string, string> listOptions = _presenter.GetListOptions();
            Dictionary<string, string> titleFieldOptions = _presenter.GetTitleFieldOptions(part.ListGuid);
            Dictionary<string, string> dateFieldOptions = _presenter.GetDateFieldOptions(part.ListGuid);

            // Create the List DDL and Options
            _list = new DropDownList();
            foreach (var option in listOptions)
                _list.Items.Add(new ListItem(option.Value, option.Key));

            // Create the TitleField DDL and Options
            //_titleField = new DropDownList();
            //foreach (var option in titleFieldOptions)
            //    _titleField.Items.Add(new ListItem(option.Value, option.Key));

            // Create the StartDateField DDL and Options
            //_startDateField = new DropDownList();
            //_endDateField = new DropDownList();
            //foreach (var option in dateFieldOptions)
            //{
            //    _startDateField.Items.Add(new ListItem(option.Value, option.Key));
            //    _endDateField.Items.Add(new ListItem(option.Value, option.Key));
            //}

            // Create the Fields Textboxes
            _titleField = new TextBox();
            _startDateField = new TextBox();
            _endDateField = new TextBox();

            // Create the PauseTime Textbox
            _pauseTime = new TextBox();

            // Create the Asset Locations DDL and Options
            _includejQueryReference = new CheckBox();
            _includebxSliderReference = new CheckBox();

            // Create the jQuery Path Textbox
            _jQueryPath = new TextBox();

            // Create the bxSlider Path Textbox
            _bxSliderPath = new TextBox();

            // Create the CSS Path Textbox
            _cssPath = new TextBox();


            Controls.Add(_list);
            Controls.Add(_titleField);
            Controls.Add(_startDateField);
            Controls.Add(_endDateField);
            Controls.Add(_pauseTime);
            Controls.Add(_includejQueryReference);
            Controls.Add(_jQueryPath);
            Controls.Add(_includebxSliderReference);
            Controls.Add(_bxSliderPath);
            Controls.Add(_cssPath);
        }


        /// <summary>
        /// Render the controls to the UI
        /// </summary>
        /// <param name="writer">HtmlTextWriter</param>
        protected override void RenderContents(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
            writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "PostItEditor");
            writer.RenderBeginTag(HtmlTextWriterTag.Table);

            // Add the List property
            AddProperty(writer, "List Name", _list, true);


            // Add the TitleField property
            AddProperty(writer, "Title Field", _titleField, false);

            // Add the StartDateField property
            AddProperty(writer, "Start Date Field", _startDateField, false);

            // Add the EndDateField property
            AddProperty(writer, "End Date Field", _endDateField, true);


            // Add the PauseTime property
            AddProperty(writer, "Pause Time", _pauseTime, true);


            // Add the jQuery Reference property
            AddCheckBoxWithTextBoxProperty(writer, "jQuery", "Include reference to jQuery library?", _includejQueryReference, "Path to jQuery library", _jQueryPath, true);


            // Add the bxSlider Reference property
            AddCheckBoxWithTextBoxProperty(writer, "bxSlider", "Include reference to bxSlider library?", _includebxSliderReference, "Path to bxSlider library", _bxSliderPath, true);

            // Add the CSS Path property
            AddProperty(writer, "Path to CSS library", _cssPath, false);


            writer.RenderEndTag();
        }


        /// <summary>
        /// Add a property to be rendered to html
        /// </summary>
        /// <param name="writer">HtmlTextWriter</param>
        /// <param name="label">String to display as label</param>
        /// <param name="control">Control to render</param>
        /// <param name="addDivider">Whether to add a divider after the property</param>
        private void AddProperty(HtmlTextWriter writer, string label, Control control, bool addDivider)
        {
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);

            // Add the Label
            AddLabel(writer, label, control.ClientID);

            // Add the Control
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "UserSectionBody");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "UserControlGroup");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            control.RenderControl(writer);

            writer.RenderEndTag();
            writer.RenderEndTag();

            // Add Divider
            if (addDivider)
                AddDivider(writer);

            writer.RenderEndTag();
            writer.RenderEndTag();
        }


        /// <summary>
        /// Add a property to be rendered to html.  Format for a checkbox
        /// </summary>
        /// <param name="writer">HtmlTextWriter</param>
        /// <param name="label">String to display as label</param>
        /// <param name="cbLabel">CheckBox label</param>
        /// <param name="cb">CheckBox control to render</param>
        /// <param name="tbLabel">TextBox label</param>
        /// <param name="tb">TextBox control to render</param>
        /// <param name="addDivider">Whether to add a divider after the property</param>
        private void AddCheckBoxWithTextBoxProperty(HtmlTextWriter writer, string label, string cbLabel, Control cb, string tbLabel, Control tb, bool addDivider)
        {
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);

            // Add the label
            AddLabel(writer, label, tb.ClientID);


            // Add the Control
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "UserSectionBody");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "UserControlGroup");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);


            // Add the CheckBox Control
            writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "2");
            writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
            writer.RenderBeginTag(HtmlTextWriterTag.Table);
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            cb.RenderControl(writer);
            writer.RenderEndTag();

            // Add the CheckBox Label
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.AddAttribute(HtmlTextWriterAttribute.For, cb.ClientID);
            writer.RenderBeginTag(HtmlTextWriterTag.Label);
            writer.Write(cbLabel);
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.RenderEndTag();


            // Add the Textbox Label
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.AddAttribute(HtmlTextWriterAttribute.For, tb.ClientID);
            writer.RenderBeginTag(HtmlTextWriterTag.Label);
            writer.Write(tbLabel);
            writer.RenderEndTag();
            writer.RenderEndTag();
            writer.RenderEndTag();


            // Add the Textbox Control
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            tb.RenderControl(writer);
            writer.RenderEndTag();
            writer.RenderEndTag();


            writer.RenderEndTag();
            writer.RenderEndTag();


            // Add Divider
            if (addDivider)
                AddDivider(writer);

            writer.RenderEndTag();
            writer.RenderEndTag();
            writer.RenderEndTag();
        }



        /// <summary>
        /// Add a property to be rendered to html
        /// </summary>
        /// <param name="writer">HtmlTextWriter</param>
        /// <param name="label">String to display as label</param>
        /// <param name="control">Control to render</param>
        /// <param name="addDivider">Whether to add a divider after the property</param>
        private void AddReferenceProperty(HtmlTextWriter writer, string label, Control control, bool addDivider)
        {
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);

            // Add the Label
            AddLabel(writer, label, control.ClientID);

            // Add the Control
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "UserSectionBody");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "UserControlGroup");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            control.RenderControl(writer);

            writer.RenderEndTag();
            writer.RenderEndTag();

            // Add Divider
            if (addDivider)
                AddDivider(writer);

            writer.RenderEndTag();
            writer.RenderEndTag();
        }


        /// <summary>
        /// Add a label to be rendered to html
        /// </summary>
        /// <param name="writer">HtmlTextWriter</param>
        /// <param name="label">String to display as label</param>
        /// <param name="labelFor">Control to associate with the label</param>
        private void AddLabel(HtmlTextWriter writer, string label, string labelFor)
        {
            // Add the Label
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "UserSectionHead");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            if (!string.IsNullOrEmpty(labelFor))
                writer.AddAttribute(HtmlTextWriterAttribute.For, labelFor);

            writer.RenderBeginTag(HtmlTextWriterTag.Label);
            writer.Write(label);
            writer.RenderEndTag();

            writer.RenderEndTag();
        }


        /// <summary>
        /// Add a dotted line to be rendered to html
        /// </summary>
        /// <param name="writer">HtmlTextWriter</param>
        private void AddDivider(HtmlTextWriter writer)
        {
            writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "100%");
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "UserDottedLine");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.RenderEndTag();
        }
    }
}
